package cn.test.livedatademo

import android.app.Service
import android.content.Intent
import android.os.IBinder
import cn.test.livedatademo.MainActivity.Companion.countLiveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlin.concurrent.thread

class MyService : Service() {

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        thread {
            work()
        }
        return super.onStartCommand(intent, flags, startId)
    }

    private fun work() {
        for (index in 1..1000) {
            GlobalScope.launch(Dispatchers.Main) {
                countLiveData.value = index
            }

            Thread.sleep(1000)
        }
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }
}